package com.itic.mall.system.users.store.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.type.UsersStoreActivityType;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.system.inter.users.store.persistence.dao.UsersStoreDao;
import com.itic.mall.system.inter.users.store.persistence.model.UsersStore;
import com.itic.mall.system.inter.users.store.persistence.model.UsersStoreExample;
import com.itic.mall.system.inter.users.store.persistence.model.UsersStoreExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UsersStoreService
     extends CrudService<UsersStoreDao,UsersStore,UsersStoreExample> {
    @Autowired
    private UsersStoreDao usersStoreDao;
    
    public UsersStore getUsersStoreInfo(String userId){
    	UsersStore usersStore = null;
    	UsersStoreExample example = new UsersStoreExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	List<UsersStore> list= this.usersStoreDao.selectByExample(example);
    	if(list.size()>0){
    		usersStore = list.get(0);
    	}
    	return usersStore;
    }
    /**
     * 查精选数据
     * @param userId
     * @param activityType
     * @param spuId
     * @return
     */
    public UsersStore getUsersStoreActivityInfo(String userId,String activityType,String spuId){
    	UsersStore usersStore = null;
    	UsersStoreExample example = new UsersStoreExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	if(StringHelper.isNotBlank(activityType)){
    		if(activityType.equals("TJ")){
    			criteria.andIsOwnerRecomEqualTo(DBConst.TRUE);
    		}
    		if(activityType.equals("JX")){
    			criteria.andIsOwnerJingxuanEqualTo(DBConst.TRUE);
    		}
    	}
    	if(StringHelper.isNotBlank(spuId)){
    		criteria.andGoodsIdEqualTo(spuId);
    	}
    	List<UsersStore> list= this.usersStoreDao.selectByExample(example);
    	if(list.size()>0){
    		usersStore = list.get(0);
    	}
    	return usersStore;
    }
    /**
     * 获取店主用户下的商品ID
     * @param userId
     * @return
     */
    public List<String> getUsersStore(String userId,String activityType){
    	List<String> goodsList = new ArrayList<String>();
    	UsersStoreExample example = new UsersStoreExample();
    	if(StringHelper.isNotBlank(userId)){
    		Criteria criteria = example.createCriteria();
        	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        	criteria.andUserIdEqualTo(userId);
        	if(StringHelper.isNotBlank(activityType)){
        		if(activityType.equals(UsersStoreActivityType.TJ.getType())){
        			criteria.andIsOwnerRecomEqualTo(DBConst.TRUE);
        		}
        		if(activityType.equals(UsersStoreActivityType.JX.getType())){
        			criteria.andIsOwnerJingxuanEqualTo(DBConst.TRUE);
        		}
        	}
        	List<UsersStore> list  = this.usersStoreDao.selectByExample(example);
        	for(UsersStore usersStore:list){
        		goodsList.add(usersStore.getGoodsId());
        	}
    	}
    	return goodsList;
    }
}